package top.yaofengqiao.springcloudsimple.auth.biz.infrastructure.repository;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Repository;
import top.yaofengqiao.springcloudsimple.auth.biz.domain.auth.model.valobj.Oauth2Type;
import top.yaofengqiao.springcloudsimple.auth.biz.domain.user.model.entity.MemberEntity;
import top.yaofengqiao.springcloudsimple.auth.biz.domain.user.repository.IMemberRepository;
import top.yaofengqiao.springcloudsimple.auth.biz.infrastructure.dao.MemberMapper;
import top.yaofengqiao.springcloudsimple.auth.biz.infrastructure.dao.MemberOauth2Mapper;
import top.yaofengqiao.springcloudsimple.auth.biz.infrastructure.po.MemberOauth2PO;
import top.yaofengqiao.springcloudsimple.auth.biz.infrastructure.po.MemberPO;

import javax.annotation.Resource;

/**
 * @author yfq
 * @date 2024/6/6 15:39
 * @description
 */
@Repository
public class MemberRepository implements IMemberRepository {
    @Resource
    private MemberMapper memberMapper;
    @Resource
    private MemberOauth2Mapper memberOauth2Mapper;

    @Override
    public MemberEntity queryByMobile(String mobile) {
        if (StrUtil.isEmpty(mobile)) return null;

        MemberPO memberPO = memberMapper.selectOne(Wrappers.<MemberPO>lambdaQuery().eq(MemberPO::getMobile, mobile));

        return buildMemberUseEntity(memberPO);
    }

    @Override
    public MemberEntity queryById(long memberId) {
        MemberPO memberPO = memberMapper.selectById(memberId);

        return buildMemberUseEntity(memberPO);
    }

    @Override
    public MemberEntity queryByOpenid(String openid, Oauth2Type oauth2Type) {
        if (StrUtil.isEmpty(openid)) return null;

        // 1. 查询三方授权信息
        MemberOauth2PO memberOauth2PO = memberOauth2Mapper.selectOne(Wrappers.<MemberOauth2PO>lambdaQuery()
                .eq(MemberOauth2PO::getOpenid, openid)
                .eq(MemberOauth2PO::getType, oauth2Type.getType()));

        if (memberOauth2PO == null) return null;

        // 2. 查询用户信息
        return queryById(memberOauth2PO.getUserId());
    }

    private MemberPO buildMemberUserPO(MemberEntity memberUserEntity) {
        if (memberUserEntity == null) return null;

        MemberPO memberUserPO = new MemberPO();
        memberUserPO.setId(memberUserEntity.getId());
        memberUserPO.setMobile(memberUserEntity.getMobile());
        memberUserPO.setPassword(memberUserEntity.getPassword());
        memberUserPO.setStatus(memberUserEntity.getStatus());
        memberUserPO.setRegisterIp(memberUserEntity.getRegisterIp());
        memberUserPO.setRegisterTerminal(memberUserEntity.getRegisterTerminal());
        memberUserPO.setLoginIp(memberUserEntity.getLoginIp());
        memberUserPO.setLoginDate(memberUserEntity.getLoginDate());
        memberUserPO.setNickname(memberUserEntity.getNickname());
        memberUserPO.setAvatar(memberUserEntity.getAvatar());
        memberUserPO.setName(memberUserEntity.getName());
        memberUserPO.setSex(memberUserEntity.getSex());
        memberUserPO.setAreaId(memberUserEntity.getAreaId());
        memberUserPO.setBirthday(memberUserEntity.getBirthday());
        memberUserPO.setMark(memberUserEntity.getMark());
        memberUserPO.setPoint(memberUserEntity.getPoint());
        memberUserPO.setTagIds(memberUserEntity.getTagIds());
        memberUserPO.setLevelId(memberUserEntity.getLevelId());
        memberUserPO.setExperience(memberUserEntity.getExperience());
        memberUserPO.setGroupId(memberUserEntity.getGroupId());
        memberUserPO.setOpenid(memberUserEntity.getOpenid());
        memberUserPO.setOauth2Type(memberUserEntity.getOauth2Type());

        return memberUserPO;
    }

    private MemberEntity buildMemberUseEntity(MemberPO memberUserPO) {
        if (memberUserPO == null) return null;

        return MemberEntity.builder()
                .id(memberUserPO.getId())
                .mobile(memberUserPO.getMobile())
                .password(memberUserPO.getPassword())
                .status(memberUserPO.getStatus())
                .registerIp(memberUserPO.getRegisterIp())
                .registerTerminal(memberUserPO.getRegisterTerminal())
                .loginIp(memberUserPO.getLoginIp())
                .loginDate(memberUserPO.getLoginDate())
                .nickname(memberUserPO.getNickname())
                .avatar(memberUserPO.getAvatar())
                .name(memberUserPO.getName())
                .sex(memberUserPO.getSex())
                .areaId(memberUserPO.getAreaId())
                .birthday(memberUserPO.getBirthday())
                .mark(memberUserPO.getMark())
                .point(memberUserPO.getPoint())
                .tagIds(memberUserPO.getTagIds())
                .levelId(memberUserPO.getLevelId())
                .experience(memberUserPO.getExperience())
                .groupId(memberUserPO.getGroupId())
                .openid(memberUserPO.getOpenid())
                .oauth2Type(memberUserPO.getOauth2Type())
                .build();
    }
}
